package icu.qx.wm.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.wm.domain.WmWarehouse;
import icu.qx.wm.service.IWmWarehouseService;
import icu.qx.wm.vo.WarehouseFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/wm/warehouse/manage")
public class WmWarehouseController extends BaseController {
    private final IWmWarehouseService wmWarehouseService;

    @Autowired
    public WmWarehouseController(IWmWarehouseService wmWarehouseService) {
        this.wmWarehouseService = wmWarehouseService;
    }

    /**
     * 导出仓库列表
     */
    @RequiresPermissions("wm:warehouse:manage:export")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmWarehouse wmWarehouse) {
        List<WmWarehouse> list = wmWarehouseService.selectWmWarehouseList(wmWarehouse);
        ExcelUtil<WmWarehouse> util = new ExcelUtil<>(WmWarehouse.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @RequiresPermissions("wm:warehouse:manage:query")
    @GetMapping(value = "/{wCode}")
    public AjaxResult getInfo(@PathVariable("wCode") String wCode) {
        return success(wmWarehouseService.selectWmWarehouseByWCode(wCode));
    }

    /**
     * 新增仓库
     */
    @RequiresPermissions("wm:warehouse:manage:add")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmWarehouse wmWarehouse) {
        return toAjax(wmWarehouseService.insertWmWarehouse(wmWarehouse));
    }

    /**
     * 修改仓库
     */
    @RequiresPermissions("wm:warehouse:manage:edit")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmWarehouse wmWarehouse) {
        return toAjax(wmWarehouseService.updateWmWarehouse(wmWarehouse));
    }

    /**
     * 删除仓库
     */
    @RequiresPermissions("wm:warehouse:manage:remove")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wCodes}/{saCount}")
    public AjaxResult remove(@PathVariable(name = "wCodes") String[] wCodes, @PathVariable(name = "saCount", required = false) Integer[] saCount) {
        return toAjax(wmWarehouseService.deleteWmWarehouseByWCodes(wCodes, saCount));
    }

    /**
     *查询仓库列表
     */
    @RequiresPermissions("wm:warehouse:manage:list")
    @GetMapping("/list")
    public TableDataInfo list(WmWarehouse wmWarehouse) {
        startPage();
        List<WmWarehouse> list = wmWarehouseService.selectWmWarehouseWithArea(wmWarehouse);
        return getDataTable(list);
    }

    /**
     * 查询仓库编码
     */
    @RequiresPermissions("wm:warehouse:manage:query")
    @GetMapping("/list/codes")
    public AjaxResult getWCodeWithSaCode(WarehouseFormVo warehouseFormVo) {
        return success(wmWarehouseService.selectWmWarehouseCode(warehouseFormVo));
    }
}
